package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.UserLike;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查看今日佳人
     *
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //创建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //创建query
        Query query = new Query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResult queryrecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建query对象
        Query query = new Query(criteria);
        //查询总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //查询数据表
        query.with(Sort.by(Sort.Order.desc("score"))).limit((page - 1) * pagesize).skip(pagesize);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page, pagesize, (int) count, recommendUsers);
    }

    //查看佳人详情
    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        //通过查找推荐用户表中用户id和推荐用户id获取两者关联的对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId).and("userId").is(userId);
        Query query = Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(99d);
        }
        return recommendUser;
    }

    /**
     * 探花-左滑右滑
     *
     * @param userId
     * @param counts
     * @return
     */
    @Override
    public List<RecommendUser> queryCardsList(Long userId, int counts) {
        //构建查询方法
        Query query = Query.query(Criteria.where("userId").is(userId));
        //通过query查询喜欢的用户
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //查询喜欢不喜欢的用户id
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //查询推荐的用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").is(likeUserId);

        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria)//指定查询的条件
                , Aggregation.sample(counts));

        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(newAggregation, RecommendUser.class);

        return aggregate.getMappedResults();
    }
}
